package com.sgb.kjwl.viewmodel.self.logistics;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.GoodsManageItemData;
import com.sgb.kjwl.model.entity.self.GoodsMangeResultEntity;
import com.swgk.core.util.UserOperating;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.GoodsManageEvent;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 杨畔
 * @date: 2020/12/28 11:10
 * @Description: 货源管理model层
 */
public class CargoManageModel extends AndroidViewModel {
    public MutableLiveData<GoodsMangeResultEntity> ldGoods = new MutableLiveData<>();

    public CargoManageModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取货源列表
     */
    public void getGoodsManagerList(Context context, int page, int state, int pattern) {
        DialogHelper.showProgressDialog(context, "加载中", 0, false);
        Map<String, Object> map = new HashMap<>();
        if (UserOperating.getInstance().isCompany()) {
            map.put("companyNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
            map.put("teamNo", "");
        } else if (UserOperating.getInstance().isTeam()) {
            map.put("companyNo", "");
            map.put("teamNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
        } else {
            map.put("terminal", 3);
        }
        map.put("page", page);
        map.put("limit", 15);
        //全部信息传不传state
        if (state >= 0) {
            map.put("state", state);
        }
//        map.put("pattern", pattern);
        NewAPIManager.getInstance().getLogisticsAPI().getGoodsManageList(map).enqueue(new ICallBack<BaseEntity<GoodsMangeResultEntity>>() {
            @Override
            public void onSuccess(BaseEntity<GoodsMangeResultEntity> data) {
                ldGoods.postValue(data.getData());
            }

            @Override
            public void onFail() {
                super.onFail();
                ldGoods.postValue(null);
            }
        });
    }

    /**
     * 上下架
     */
    public void downAndUpGoods(Context context, GoodsManageItemData itemData) {
        DialogHelper.showProgressDialog(context, "请稍等...");
        NewAPIManager.getInstance().getLogisticsAPI().upAndDownGoods(itemData.getNo()).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                EventBus.getDefault().post(GoodsManageEvent.make().setMessageTag(EventConstant.UP_AND_DOWN_SUCCESS));
            }
        });
    }

    /**
     * 删除
     */
    public void deleteGoods(Context context, GoodsManageItemData itemData) {
        DialogHelper.showProgressDialog(context, "请稍等...");
        NewAPIManager.getInstance().getLogisticsAPI().deleteGoods(String.valueOf(itemData.getId())).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                if ("200".equals(data.getCode())) {
                    EventBus.getDefault().post(GoodsManageEvent.make().setMessageTag(EventConstant.DELETE_SUCCESS));
                } else {
                    MToast.showToast(getApplication(), "删除失败");
                }
            }
        });
    }
}
